package com.open.pokergames.biz.views.groups.adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.open.pokergames.NetConfig;
import com.open.pokergames.R;
import com.open.pokergames.biz.entity.GroupEntity;
import com.sinothk.android.utils.XUtils;
import com.sinothk.android.utils.inters.OnSuperListener;
import com.sinothk.view.image.rounded.RoundedImageView;

import java.util.ArrayList;
import java.util.List;

public class GroupListAdapter extends RecyclerView.Adapter<GroupListAdapter.ViewHolder> {

    // 展示数据
    private ArrayList<GroupEntity> mData;
    private Context mContext;
    private OnSuperListener<GroupEntity> onSuperListener;

    public GroupListAdapter(Context mContext) {
        this.mContext = mContext;
        this.mData = new ArrayList<>();
    }

    public void setOnSuperListener(OnSuperListener<GroupEntity> onSuperListener) {
        this.onSuperListener = onSuperListener;
    }

    public void setData(List<GroupEntity> data) {
        if (mData == null) {
            mData = new ArrayList<>();
        } else {
            mData.clear();
        }
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public void updateData(List<GroupEntity> data) {
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.group_list_adapter_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // 绑定数据
        final GroupEntity itemData = mData.get(position);
        Glide.with(mContext).load(NetConfig.getImageUrl(itemData.getGroupAvatar()))
                .placeholder(R.drawable.group_avatar)
                .error(R.drawable.group_avatar)
                .into(holder.imageView);

        holder.groupNameTv.setText(XUtils.string().getNotNullValue(itemData.getGroupName()));

        holder.groupSizeTv.setText("" + itemData.getGroupMemberSize() + "/" + itemData.getGroupMemberMax());
        holder.descTv.setText(XUtils.string().getNotNullValue(itemData.getGroupDesc(), "无俱乐部描述"));

        holder.linkValueTv.setText(XUtils.string().getNotNullValue(itemData.getLinkType()) + " " +XUtils.string().getNotNullValue(itemData.getLinkValue()));

//
//        String timeStr = "";
//        if (itemData.getReleaseTime() == null) {
//            timeStr = "最近";
//        } else {
//            timeStr = XUtils.date().getFriendlyDate(itemData.getReleaseTime());
//        }
//        holder.timeTv.setText(timeStr);
//
//        holder.ptSourceTv.setText(itemData.getPtSource());
////        holder.readTimesTv.setText("阅读 "+itemData.getBrowseTimes());
//
//        if (XUtils.string().isEmpty(itemData.getImgUrl())) {
//            holder.imageView.setVisibility(View.GONE);
////            holder.imageView.setImageResource(R.drawable.icon_hone_03);
//        }else{
//            holder.imageView.setVisibility(View.VISIBLE);
//            Picasso.get().load(itemData.getImgUrl())
//                    .error(R.drawable.default_img)
//                    .placeholder(R.drawable.default_img)
//                    .into(holder.imageView);
//        }
//
        if (onSuperListener != null) {
            holder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onSuperListener.onClick(position, itemData, "rootView");
                }
            });
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout rootView;
        TextView groupNameTv, linkValueTv, descTv, groupSizeTv, jianBaoTv, scoreTv;
        RoundedImageView imageView;

        ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);
            imageView = itemView.findViewById(R.id.imageView);

            groupNameTv = itemView.findViewById(R.id.groupNameTv);
            linkValueTv = itemView.findViewById(R.id.linkValueTv);
            descTv = itemView.findViewById(R.id.descTv);
            groupSizeTv = itemView.findViewById(R.id.groupSizeTv);
        }
    }
}